﻿using DevExpress.Blazor;
using DevExpress.ExpressApp.Blazor.Components.Models;
using EasyXaf.DynamicForm.Models;

namespace EasyXaf.DynamicForm.Blazor.Components;

public class TextBoxComponent : FieldComponent
{
    protected override object ConvertValue(object value)
    {
        return base.ConvertValue(value)?.ToString();
    }

    protected override ComponentModelBase CreateComponentModel()
    {
        if (Field.Attributes.TryGetValue("data-source", out var dataSource))
        {
            return new DxComboBoxModel<string, string>
            {
                Id = Field.Id,
                Data = dataSource.Split(',').Select(x => x.Trim()).ToArray(),
            };
        }
        else if (Field.Attributes.TryGetValue("rows", out var rows))
        {
            var model = new DxMemoModel
            {
                Id = Field.Id,
                ResizeMode = MemoResizeMode.Vertical,
            };

            if (int.TryParse(rows, out var value))
            {
                model.Rows = value;
            }

            return model;
        }
        else
        {
            var model = new DxTextBoxModel
            {
                Id = Field.Id,
            };

            if (Field.Attributes.TryGetValue("password", out string password) && password == "true")
            {
                model.Password = true;
            }

            return model;
        }
    }
}
